#!/bin/bash

# Author: Turing Zhu<qishiwenjun@163.com>
# Date: 2018-06-13 18:51:37
# Desc: stop jar
#

# error code
# error code 
# 1: missing jarfile
# 2: stop failed


function print_help() {
    cat <<EOF
Stop the specified jar process
Usage:      $0 jarfile
Example:    $0 abc.jar

EOF
}

function main() {
    # check parameter
    if [[ $# -lt 1 ]];then
		printf "[ \033[31mERROR 1\033[0m ] missing parameter\n"
        print_help
        exit 1
    fi
	# get pid
	local jarfile=$1
	local pid=$(ps -ef|grep -E "\-jar $jarfile" | grep -v grep |awk '{print $2}')
	if [[ "$pid" == "" ]];then
		printf "[ \033[33mWARNING\033[0m ] not found process about $jarfile. does it not running?\n"
		exit 0
	fi

	kill -9 $pid
	if [[ $? -ne 0 ]];then
		printf "[ \033[31mERROR 2\033[0m ] stop $jarfile failed.\n"
		exit 2
	fi
	printf "\033[32m STOP $pid of $jarfile SUCCESS. \033[0m\n"
}


main $*
